Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
int64-buffer
Advanced tools
The int64-buffer npm package provides utilities for handling 64-bit integers in JavaScript. It allows you to create, manipulate, and convert 64-bit integers, which are not natively supported by JavaScript's number type.
Creating Int64 and Uint64 instances
This feature allows you to create instances of 64-bit signed and unsigned integers using high and low 32-bit parts.
const Int64 = require('int64-buffer').Int64;
const Uint64 = require('int64-buffer').Uint64;
const int64 = new Int64(0x12345678, 0x9abcdef0);
const uint64 = new Uint64(0x12345678, 0x9abcdef0);
console.log(int64.toString()); // '1311768467463790320'
console.log(uint64.toString()); // '1311768467463790320'
Converting between different formats
This feature allows you to convert 64-bit integers to different formats such as Buffer, hexadecimal string, and JavaScript number.
const Int64 = require('int64-buffer').Int64;
const int64 = new Int64(0x12345678, 0x9abcdef0);
const buffer = int64.toBuffer();
const hexString = int64.toString(16);
const number = int64.toNumber();
console.log(buffer); // <Buffer 12 34 56 78 9a bc de f0>
console.log(hexString); // '123456789abcdef0'
console.log(number); // 1311768467463790320
Reading and writing from/to Buffers
This feature allows you to read 64-bit integers from Buffers and write 64-bit integers to Buffers.
const Int64 = require('int64-buffer').Int64;
const buffer = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0]);
const int64 = new Int64(buffer);
console.log(int64.toString()); // '1311768467463790320'
const newBuffer = Buffer.alloc(8);
int64.toBuffer().copy(newBuffer);
console.log(newBuffer); // <Buffer 12 34 56 78 9a bc de f0>
The 'long' package provides a Long class for representing a 64-bit two's-complement integer. It offers similar functionality to int64-buffer, including arithmetic operations, bitwise operations, and conversion to/from various formats. However, 'long' is more focused on arithmetic operations and less on buffer manipulation.
The 'int64' package provides a way to work with 64-bit integers in JavaScript. It offers similar functionality to int64-buffer, including creating 64-bit integers, converting to/from buffers, and performing arithmetic operations. The main difference is that 'int64' is designed to be simpler and more lightweight.
The 'big-integer' package provides arbitrary-precision integers in JavaScript. While it can handle 64-bit integers, it is more general-purpose and can handle integers of any size. It offers a wide range of arithmetic and bitwise operations, but it is not specifically optimized for 64-bit integers like int64-buffer.
64bit Long Integer on Buffer/Array/ArrayBuffer in Pure JavaScript
JavaScript's number based on IEEE-754 could only handle 53 bits precision.
This module provides two pair of classes: Int64BE
/Uint64BE
and Int64LE
/Uint64LE
which could hold 64 bits long integer and loose no bit.
Int64BE
/Int64LE
for signed integer, Uint64BE
/Uint64LE
for unsigned.Int64BE
/Uint64BE
for big-endian, Uint64BE
/Uint64LE
for little-endian.Buffer
/Uint8Array
/Array
/Array
-like storage of 8 bytes length with offset.add()
, sub()
, mul()
, div()
etc.Int64BE
is the class to host a 64 bit signed long integer int64_t
.
var Int64BE = require("int64-buffer").Int64BE;
var big = new Int64BE(-1);
console.log(big - 0); // -1
console.log(big.toBuffer()); // <Buffer ff ff ff ff ff ff ff ff>
It uses Buffer
on Node.js and Uint8Array
on modern Web browsers.
Uint64BE
is the class to host a 64 bit unsigned positive long integer uint64_t
.
var Uint64BE = require("int64-buffer").Uint64BE;
var big = new Uint64BE(Math.pow(2, 63)); // a big number with 64 bits
console.log(big - 0); // 9223372036854776000 = IEEE-754 loses last bits
console.log(big + ""); // "9223372036854775808" = perfectly correct
Int64LE
and Uint64LE
work as same as above but with little-endian storage.
var big = new Uint64BE(1234567890);
console.log(big - 0); // 1234567890
var big = new Uint64BE(0x12345678, 0x9abcdef0);
console.log(big.toString(16)); // "123456789abcdef0"
var big = new Uint64BE("123456789abcdef0", 16);
console.log(big.toString(16)); // "123456789abcdef0"
var buffer = new Buffer([1,2,3,4,5,6,7,8]);
var big = new Uint64BE(buffer);
console.log(big.toString(16)); // "102030405060708"
var uint8array = new Uint8Array([1,2,3,4,5,6,7,8]);
var big = new Uint64BE(uint8array);
console.log(big.toString(16)); // "102030405060708"
var arraybuffer = (new Uint8Array([1,2,3,4,5,6,7,8])).buffer;
var big = new Uint64BE(arraybuffer);
console.log(big.toString(16)); // "102030405060708"
var array = [1,2,3,4,5,6,7,8];
var big = new Uint64BE(array);
console.log(big.toString(16)); // "102030405060708"
var buffer = new Buffer([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
var big = new Uint64BE(buffer, 8);
console.log(big.toString(16)); // "90a0b0c0d0e0f10"
var buffer = new Buffer(16);
var big = new Uint64BE(buffer, 8, 0x1234567890);
console.log(big.toString(16)); // "1234567890"
console.log(buffer[15].toString(16)); // "90"
var buffer = new Uint8Array(16);
var big = new Uint64BE(buffer, 8, 0x12345678, 0x9abcdef0);
console.log(big.toString(16)); // "123456789abcdef0"
console.log(buffer[15].toString(16)); // "f0"
var buffer = new Array(16);
var big = new Uint64BE(buffer, 8, "123456789abcdef0", 16);
console.log(big.toString(16)); // "123456789abcdef0"
console.log(buffer[15].toString(16)); // "f0"
var big = Uint64BE(1234567890);
console.log(big - 0); // 1234567890
var big = Uint64BE(1234567890);
console.log(big + ""); // "1234567890"
var big = Uint64BE();
console.log(JSON.stringify({big: big})); // {"big":1234567890}
var big = Uint64BE(1234567890);
console.log(big.toNumber()); // 1234567890
var big = Uint64BE(0x1234567890);
console.log(big.toString()); // "78187493520"
console.log(big.toString(16)); // "1234567890"
var big = Uint64BE([1,2,3,4,5,6,7,8]);
console.log(big.toBuffer()); // <Buffer 01 02 03 04 05 06 07 08>
var big = Uint64BE(0);
var buf = new Int8Array(big.toArrayBuffer());
console.log(buf); // Int8Array { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5, '5': 6, '6': 7, '7': 8 }
var big = Uint64BE([1,2,3,4,5,6,7,8]);
console.log(big.toArray()); // [ 1, 2, 3, 4, 5, 6, 7, 8 ]
int64-buffer.min.js is tested on major Web browsers.
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="https://rawgit.com/kawanet/int64-buffer/master/dist/int64-buffer.min.js"></script>
<script>
var i = Int64BE("1234567890123456789");
console.log(i.toString(10)); // "1234567890123456789"
var u = new Uint64BE([0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF]);
console.log(u.toString(16)); // "123456789abcdef"
</script>
npm install int64-buffer --save
Copyright (c) 2015-2017 Yusuke Kawasaki
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
64bit Long Integer on Buffer/Array/ArrayBuffer in Pure JavaScript
The npm package int64-buffer receives a total of 2,026,964 weekly downloads. As such, int64-buffer popularity was classified as popular.
We found that int64-buffer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.